import json
import datetime

from django.http import JsonResponse


def not_support_response():
    res = {"code": 400, "msg": "不支持的请求!检查一下请求方法、url是否合法"}
    return JsonResponse(res, safe=False, content_type='application/json; charset=utf-8')


def no_upload_file_response():

    res = {"code": 400, "msg": "你还没有上传文件!"}
    return JsonResponse(res, safe=False, content_type='application/json; charset=utf-8')


def failed_response_with_data(data=None, msg=None, code=400):
    res = {
        "code": code,
        "msg": msg,
        "data": data,

    }
    if msg is None:
        res["msg"] = "执行失败"
    if data is None:
        res["data"] = {}
    return JsonResponse(res, content_type='application/json; charset=utf-8', safe=False)


def success_response_with_data(data=None, msg=None):
    res = {
        "code": 200,
        "msg": msg,
        "data": data,

    }
    if msg is None:
        res["msg"] = "执行成功"
    if data is None:
        res["data"] = {}
    return JsonResponse(res, content_type='application/json; charset=utf-8', safe=False)


def list_response(obj_info=None, total=None, pagesize=None, pagenum=None, table_name=None):
    res = {
        "code": 200,
        "msg": "列表查询成功",
        "data": {}
    }
    data_part = res["data"]
    if table_name is not None:
        data_part["table_name"] = table_name
    if total is not None:
        data_part["total"] = total
    if pagesize is not None:
        data_part["pagesize"] = pagesize
    if pagenum is not None:
        data_part["pagenum"] = pagenum
    if obj_info is None:
        data_part["list"] = []
    else:
        data_part["list"] = obj_info
    return JsonResponse(res, safe=False, content_type='application/json; charset=utf-8')


def get_sex_zh(sex):
    if sex == 'male':
        return '男'
    if sex == 'female':
        return '女'
    return sex


def current_time():
    return datetime.datetime.now().strftime("%Y%m%d%H%M%S")


# 去除path路径开始的斜杠，###如果还有，继续去除
def exclude_slash_static(path):
    s_path = path
    if s_path.startswith("/static"):
        return s_path[7:]
    elif s_path.startswith("static"):
        return s_path[6:]
    return s_path


# 去除path路径开始的斜杠，###如果还有，继续去除
def remove_first_slash(path):
    s_path = path
    while s_path.startswith("/"):
        s_path = s_path[1:]
    return s_path


# 去除path路径最后的斜杠  ###如果还有，继续去除
def remove_last_slash(path):
    s_path = path
    while s_path.endswith("/"):
        s_path = s_path[:-1]
    return s_path
